﻿using System;
using System.IO;
using System.Text;

namespace Raffles.OA.Utility
{
    /// <summary>
    /// 
    /// </summary>
    public class CommonHelper
    {
        /// <summary>
        /// 表示全局唯一标识符 (GUID)。
        /// </summary>
        /// <returns></returns>
        public static string GuId()
        {
            return Guid.NewGuid().ToString();
        }

        /// <summary>
        /// 自动生成随机码
        /// </summary>
        /// <param name="Length">随机码长度</param>
        /// <returns></returns>
        public static string CreateRandomCode(int Length = 4)
        {
            string RandomCode = string.Empty;

            char[] character = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

            Random rnd = new Random();

            //生成随机码字符串 
            for (int i = 0; i < Length; i++)
            {
                RandomCode += character[rnd.Next(character.Length)];
            }

            return RandomCode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Text"></param>
        /// <param name="salt"></param>
        /// <returns></returns>
        public static string CreatePassword(string Text, string salt)
        {
            return Md5Helper.md5(DESEncrypt.Encrypt(Md5Helper.md5(Text).ToLower() + Md5Helper.md5(salt).ToLower())).ToLower();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="num"></param>
        /// <param name="length"></param>
        /// <param name="paddingChar"></param>
        /// <returns></returns>
        public static string FormatNum(int num, int length = 4, char paddingChar = '0')
        {
            return num.ToString().PadLeft(length, paddingChar);
        } 
    }
}
